<?php

class InventoryMovement
{
	
	public function receivePurchaseOrder($oPurchaseOrder)
	{
		$line_items = PurchaseOrderLineItems::findLineItems($oPurchaseOrder->id);
		
		foreach ($line_items as $line_item)
		{
			$item_id = $line_item->item->id;
			$item_quantity = $line_item->quantity;
			
			ItemQuantity::adjustItemQuantity($item_id, $item_quantity);
			
			$oBatch = new InventoryBatch;
			$oBatch->item_id = $item_id;
			$oBatch->location_id = $oPurchaseOrder->location_id;
			$oBatch->model = get_class($oPurchaseOrder);
			$oBatch->model_id = $oPurchaseOrder->id;
			$oBatch->quantity = $item_quantity;
			$oBatch->unit_cost = $line_item->item->getCost();
			$oBatch->quantity_on_hand = $item_quantity;
			$oBatch->date_created = date("Y-m-d H:i:s");
			$oBatch->save();

		}
	}
	
	public function adjustWorkOrderQuantity($itemId, $woId, $quantityAdjustment)
	{
		$oWorkOrder = WorkOrder::model()->findByPk($woId);
		
		if (InventoryBatch::batchRecordExists($itemId, $oWorkOrder))
		{
			$oBatch = InventoryBatch::findItemBatch($itemId, $oWorkOrder);
			$oBatch->quantity += $quantityAdjustment;
			$oBatch->quantity_on_hand += $quantityAdjustment;
			$oBatch->save();
		}
		else 
		{
			$oItem = Item::model()->findByPk($itemId);
			
			$oBatch = new InventoryBatch;
			$oBatch->item_id = $itemId;
			$oBatch->location_id = $oWorkOrder->location_id;
			$oBatch->model = get_class($oWorkOrder);
			$oBatch->model_id = $oWorkOrder->id;
			$oBatch->quantity = $quantityAdjustment;
			$oBatch->unit_cost = $oItem->getCost();
			$oBatch->quantity_on_hand = $quantityAdjustment;
			$oBatch->date_created = date("Y-m-d H:i:s");
			$oBatch->save();
		}
		
		$inventory_method = OrganizationSettings::model()->getInventoryMethod(Yii::app()->user->getState('organization_id'));
		
		// get bill of materials
		$oBom = new BillOfMaterials;
		$components = $oBom->buildBillOfMaterials($itemId);
		
		// calulcate total of each material
		foreach ($components as $component)
		{
			$component_adjustment = 0;
			
			$component_adjustment -= ($component->quantity * $quantityAdjustment);
			
			InventoryBatch::adjustBatchQuantity($component->component_id, $component_adjustment, $inventory_method);
			
			ItemQuantity::adjustItemQuantity($component->component_id, $component_adjustment);	

		}
		
	}
	
	public function adjustQuantityReceived($itemId, $originalQuantity, $currentQuantity)
	{
		// get difference
		$quantity_adjustment = ($currentQuantity - $originalQuantity);
		
		// get bill of materials
		$oBom = new BillOfMaterials;
		$components = $oBom->buildBillOfMaterials($itemId);
		
		// calulcate total of each material
		foreach ($components as $component)
		{
			$component_adjustment = 0;
			$component_adjustment -= ($component->quantity * $quantity_adjustment);
			ItemQuantity::adjustItemQuantity($component->component_id, $component_adjustment);	
		}

		ItemQuantity::adjustItemQuantity($itemId, $quantity_adjustment);
		
	}
	
}